﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Erau.ERBParser
{
	/// <summary>
	/// エラー箇所を表示するための位置データ。整形前のデータなのでエラー表示以外の理由で参照するべきではない。
	/// </summary>
	public class ScriptPosition : IEquatable<ScriptPosition>, IEqualityComparer<ScriptPosition>
	{
		public ScriptPosition(string srcLine)
		{
			LineNo = -1;
			RowLine = srcLine;
			Filename = "";
		}
		public ScriptPosition(string srcFile, int srcLineNo, string srcLine)
		{
			LineNo = srcLineNo;
			RowLine = srcLine;
			if (srcFile == null)
				Filename = "";
			else
				Filename = srcFile;
		}
		public readonly int LineNo;
		public readonly string RowLine;
		public readonly string Filename;
		public override string ToString()
		{
			if (LineNo == -1)
				return base.ToString();
			return Filename + ":" + LineNo.ToString();
		}

		#region IEqualityComparer<ScriptPosition> メンバ

		public bool Equals(ScriptPosition x, ScriptPosition y)
		{
			if ((x == null) || (y == null))
				return false;
			return ((x.Filename == y.Filename) && (x.LineNo == y.LineNo));
		}

		public int GetHashCode(ScriptPosition obj)
		{
			return Filename.GetHashCode() ^ LineNo.GetHashCode();
		}

		#endregion

		#region IEquatable<ScriptPosition> メンバ

		public bool Equals(ScriptPosition other)
		{
			return this.Equals(this, other);
		}

		#endregion
	}
}
